package org.standard;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class GameCanvas extends Canvas implements Runnable, KeyListener {

	private Image screen;
	private Graphics2D graphics;
	private static int cnt = 0;
	private static int dis = 8;

	public GameCanvas() {
		screen = Utility.createImage(500, 500, true);
		this.setPreferredSize(new Dimension(500, 500));
		graphics = (Graphics2D) screen.getGraphics();
		drawPic();
		this.addKeyListener(this);
		//this.mainLoop();
	}

	public void paint(Graphics g) {
		g.drawImage(screen, 0, 0, null);
		g.dispose();
	}

	public void update(Graphics g) {
		paint(g);
	}

	public void drawPic() {
		// this.update(graphics);
		FileInputStream in = null;
		FileInputStream arrow = null;
		try {
			in = new FileInputStream(
					"/home/zane/workspace/SGDefense/src/atk.bmp");
			arrow = new FileInputStream(
					"/home/zane/workspace/SGDefense/src/arrow.bmp");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Image image = BMPLoader.read(in);
		Image arrowImage = BMPLoader.read(arrow);
		Image list[] = new Image[12];
		for (int i = 0; i < 12; i++) {
			list[i] = Utility.createImage(64, 64, true);
			Graphics g = list[i].getGraphics();
			g.drawImage(image, 0, 0, 64, 64, 0, i * 64, 64, (i + 1) * 64, null);
			g.dispose();
		}
		graphics.drawImage(list[cnt], 0, 0, null);
		graphics.drawImage(arrowImage, dis * 32, dis * 32, null);
		graphics.drawImage(Utility.createImage(32, 32, true), (dis + 1) * 32,
				(dis + 1) * 32, null);
		this.repaint();
	}

	public void mainLoop() {
		Thread gameLoop = new Thread(this);
		gameLoop.start();
	}

	@Override
	public void run() {
		// for (;;) {
		// if (cnt > 10) {
		// break;
		// }
		// System.out.println(cnt);
		// long start = System.currentTimeMillis();
		// long end = System.currentTimeMillis();
		// long time = end - start;
		// long sleepTime = 20L - time;
		// if (sleepTime < 0L) {
		// sleepTime = 0L;
		// }
		// cnt++;
		// drawPic();
		// try {
		// Thread.sleep(sleepTime);
		// } catch (InterruptedException e) {
		// }
		// }
//		for (int i = 0; i < 8; i++) {
//			try {
//				Thread.sleep(100);
//				cnt++;
//				dis--;
//				drawPic();
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

	}

	private void eventClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		switch (e.getKeyCode()) {
		case KeyEvent.VK_UP:
			cnt++;
			dis--;
			drawPic();
			break;
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

}
